package cfca.paperless.client.servlet.customer;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.FileUtils;

import cfca.paperless.client.bean.ChannelBean;
import cfca.paperless.client.bean.PdfBean;
import cfca.paperless.client.bean.SealUserInfo;
import cfca.paperless.client.bean.SignLocation;
import cfca.paperless.client.servlet.PaperlessClient;
import cfca.paperless.client.servlet.PaperlessConfig;
import cfca.paperless.client.util.Base64;
import cfca.paperless.client.util.GUID;
import cfca.paperless.client.util.IoUtil;
import cfca.paperless.client.util.PaperlessUtil;
import cfca.paperless.client.util.StrategyUtil;
import cfca.paperless.client.util.StringUtil;

public class testCompoundSealAutoPdfList_Webank {

    public static void main(String[] args) throws Exception {
        long start = System.currentTimeMillis();
        try {
            PaperlessClient clientBean = new PaperlessClient("http://127.0.0.1:8080/BizFep/BizFepServlet", 10000, 60000);
            
            ///////////////////////////如下为加盖机构印章的策略/////////////////////////////////////////////////
            List<SignLocation> signLocationList2 = new ArrayList<SignLocation>();
            SignLocation signLocation = new SignLocation(-1, "银行签章", "D", 20, 0);//加盖机构印章的位置，代表在第一个pdf文件中的“产品名称”的下方向上偏移50像素
            signLocationList2.add(signLocation);
            SealUserInfo sealUserInfo = new SealUserInfo("机构", "北京", "签合同");//印章基本信息
            String sealCode = "org001";//印章编码，在后台印章管理里可以查询到该印章
            String sealPassword = "11111111";//印章密码
            //产生普通签章策略文件，并加到clientBean中，此策略文件后台会根据sealCode查找印章来盖章
            String sealStrategyXml = StrategyUtil.createSealStrategyXml(sealCode, sealPassword, sealUserInfo, signLocationList2);
            //如下是pdf列表参数，<Pdf>里面的值可以是pdf文件的base64编码，也可以是pdf的磁盘的路径
            //<OutputFilePath>里面是pdf盖章后存放的地址，可以为空，如果不为空盖章后此pdf可以在单据稽核中查询到
            String pdf1 = Base64.toBase64String(FileUtils.readFileToByteArray(new File("./TestData/synthesizeAutoTemplate.pdf")));
            
            String bizTypeCode = "B001";//业务类型，比如：开户、开通网银
            
            List<PdfBean> pdfBeanList = new ArrayList<PdfBean>();
            String bizSerialNo = GUID.generateId();
            String outputFilePath = "/cfca/paperless/Webank/2017/" + bizSerialNo + ".pdf";
            pdfBeanList.add(new PdfBean(pdf1, bizSerialNo, bizTypeCode, outputFilePath));
            
            String pdfBeanListXml = PaperlessUtil.buildPdfBeanListXml(pdfBeanList);

            //调用自动化复合签章pdf列表方法
            String multiData = "";
            String compoundSealStrategyXml = "<List>" + sealStrategyXml + "</List>";
            String channelBeanXml = new ChannelBean(PaperlessConfig.channelCode).toString();
            String sealedPdf = clientBean.compoundSealAutoPdfList(pdfBeanListXml, multiData, compoundSealStrategyXml, PaperlessConfig.operatorCode, channelBeanXml);
            String code = StringUtil.getNodeText(sealedPdf, "Code");
            //返回值Code等于200代表后台处理成功
            if (code.equals("200")) {
                List<String[]> list = getPdfBeanArr(sealedPdf);
                for (int i = 0; i < list.size(); i++) {
                    String[] strArr = list.get(i);
                    IoUtil.write("./TestData/output/compoundSealAutoPdfList-" + i + ".pdf", Base64.decode(strArr[0].getBytes()));
                    System.out.println("BizSerialNo =" + strArr[1]);
                    System.out.println("FileId=" + strArr[2]);
                    System.out.println("FileHashValue=" + strArr[3]);
                }

            } else {
                System.out.println(sealedPdf);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("time:" + (System.currentTimeMillis() - start));
    }

    private static List<String[]> getPdfBeanArr(String pdfs) {
        List<String[]> list = new ArrayList<String[]>();
        Object pdfArr[] = StringUtil.getXmlFieldArr(pdfs, "PdfBean");
        if (pdfArr != null && pdfArr.length > 0) {
            for (int i = 0; i < pdfArr.length; i++) {
                String pdfBeanString = "<PdfBean>" + pdfArr[i].toString() + "</PdfBean>";
                String pdf = StringUtil.getXmlField(pdfBeanString, "Pdf");
                String bizSerialNo = StringUtil.getXmlField(pdfBeanString, "BizSerialNo");
                if (StringUtil.isEmpty(bizSerialNo)) {
                    bizSerialNo = "";
                }
                String fileId = StringUtil.getXmlField(pdfBeanString, "FileId");
                if (StringUtil.isEmpty(fileId)) {
                    fileId = "";
                }
                String fileHashValue = StringUtil.getXmlField(pdfBeanString, "FileHashValue");
                if (StringUtil.isEmpty(fileHashValue)) {
                    fileHashValue = "";
                }
                list.add(new String[] {pdf, bizSerialNo, fileId, fileHashValue});
            }
        }
        return list;
    }
}
